package checker

import "math/big"

var javaByteMin = new(big.Float)
var javaByteMax = new(big.Float)
var javaShortMin = new(big.Float)
var javaShortMax = new(big.Float)
var javaCharMin = new(big.Float)
var javaCharMax = new(big.Float)
var javaIntMin = new(big.Float)
var javaIntMax = new(big.Float)
var javaLongMin = new(big.Float)
var javaLongMax = new(big.Float)
var javaFloatMin = new(big.Float)
var javaFloatMax = new(big.Float)
var javaDoubleMin = new(big.Float)
var javaDoubleMAX = new(big.Float)

func init() {
	javaByteMin.SetString("-128")
	javaByteMax.SetString("127")
	javaShortMin.SetString("-32768")
	javaShortMax.SetString("32767")
	javaCharMin.SetString("0")
	javaCharMax.SetString("65535")
	javaIntMin.SetString("-2147483648")
	javaIntMax.SetString("2147483647")
	javaLongMin.SetString("-9223372036854775808")
	javaLongMax.SetString("9223372036854775807")
	javaFloatMin.SetString("1.4e-45")
	javaFloatMax.SetString("3.4028235e+38")
	javaDoubleMin.SetString("4.9e-324")
	javaDoubleMAX.SetString("1.7976931348623157e+308")
}

func inValueRange(kind TypeFlags, value *big.Float) bool {
	switch kind {
	case TFChar:
		return inCharRange(value)
	case TFByte:
		return inByteRange(value)
	case TFShort:
		return inShortRange(value)
	case TFInt:
		return inIntRange(value)
	case TFLong:
		return inLongRange(value)
	case TFFloat:
		return inFloatRange(value)
	case TFDouble:
		return inDoubleRange(value)
	}

	return false
}

func inByteRange(value *big.Float) bool {
	return value.Cmp(javaByteMin) >= 0 && value.Cmp(javaByteMax) <= 0
}

func inShortRange(value *big.Float) bool {
	return value.Cmp(javaShortMin) >= 0 && value.Cmp(javaShortMax) <= 0
}

func inCharRange(value *big.Float) bool {
	return value.Cmp(javaCharMin) >= 0 && value.Cmp(javaCharMax) <= 0
}

func inIntRange(value *big.Float) bool {
	return value.Cmp(javaIntMin) >= 0 && value.Cmp(javaIntMax) <= 0
}

func inLongRange(value *big.Float) bool {
	return value.Cmp(javaLongMin) >= 0 && value.Cmp(javaLongMax) <= 0
}

func inFloatRange(value *big.Float) bool {
	return value.Cmp(javaFloatMin) >= 0 && value.Cmp(javaFloatMax) <= 0
}

func inDoubleRange(value *big.Float) bool {
	return value.Cmp(javaDoubleMin) >= 0 && value.Cmp(javaDoubleMAX) <= 0
}

